(function($, window) {
	var $window = $(window);
	$.fn.waterfall = function(opts) {
		var wBox = $(this),
			wItem = wBox.find('li'),
			LOCALDATA = new Array,
			scrollMark = true;
		$.extend({
			wWidth: wItem.outerWidth(),
			wAnimateTime: false,
			wAddclass: null,
			Init: function() {
				var thiss = this;
				LOCALDATA = thiss.wGetPosition()
				this.wDelayShow(wItem);
				this.wScrollNew();
			},
			wGetPosition: function() {
				var thisData = new Array();
				var row = parseInt($window.width() / this.wWidth);
				wBox.width((this.wWidth + 10) * row + 10);
				for (var i = 0; i < row; i++) {
					thisData.push({
						left: (this.wWidth + 10) * i + 10,
						top: 10,
						height: 10
					});
				}
				return thisData;
			},
			wShowItem: function(item) {
				var thiss = this;

				var maxHeight = 0,
					minHeightIndex = 0;
				$.each(LOCALDATA, function(i, val) {
					minHeightIndex = LOCALDATA[minHeightIndex].height > val.height ? i : minHeightIndex;
				});

				item.css({
					left: LOCALDATA[minHeightIndex].left,
					top: LOCALDATA[minHeightIndex].top
				});
				if (thiss.wAddclass) {
					item.addClass(thiss.wAddclass);
				}
				thiss.wImgLoad(item.find('img').attr('src'), function() {
					if (typeof thiss.wAnimateTime == 'boolean') {
						item.show();
					} else {
						item.fadeIn(thiss.wAnimateTime);
					}

					LOCALDATA[minHeightIndex].top = LOCALDATA[minHeightIndex].top + item.outerHeight() + 10;
					LOCALDATA[minHeightIndex].height = LOCALDATA[minHeightIndex].height + item.outerHeight() + 10;

					$.each(LOCALDATA, function(i, val) {
						maxHeight = val.top > maxHeight ? val.top : maxHeight;
					});
					wBox.height(maxHeight);
				});
			},
			wImgLoad: function(url, callback) {
				var img = new Image();
				img.src = url;

				if (img.complete) {
					callback.call(img);
					return;
				}
				img.onload = function() {
					callback.call(img);
				};
			},
			wDelayShow: function(items) {
				var thiss = this,
					itemLength = items.length;
				var iTime = 0;
				scrollMark = false;

				var interV = setInterval(function() {
					if (iTime < itemLength) {
						thiss.wShowItem(items.eq(iTime));
						iTime++;
					} else {
						scrollMark = true;
						clearInterval(interV);
					}

				}, typeof thiss.wAnimateTime == 'boolean' ? 50 : thiss.wAnimateTime);
			},
			wScrollNew: function() {
				var thiss = this;
				$window.on('scroll', function() {
					var sTop = $(this).scrollTop() + $window.height(),
						htmls = '';
					if (sTop >= wBox.offset().top + wBox.height() && scrollMark) {
						htmls = $.isFunction(thiss.wScrollCall) ? thiss.wScrollCall(thiss, wBox) : thiss.wScrollCall;
						$(htmls).appendTo(wBox.find('ul')).ready(function() {
							thiss.wDelayShow(wBox.find('li:hidden'));
						});
					}

				});
			},
			wScrollCall: null

		}, opts).Init();
	}
})(jQuery, window)